<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>登录</span>
      </div>
      <el-form
        label-position="left"
        :rules="rules"
        ref="LoginForm"
        :model="form"
        label-width="60px"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="el-icon-s-goods"
            placeholder="请输入密码"
            v-model="form.password"
            show-password
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="warning" @click="login" :loading="IsLoading"
              >登录</el-button
            >
            <el-button @click="reset">重置</el-button>
          </el-button-group>
        </el-form-item>
        <el-checkbox label="记住密码" v-model="form.rem"></el-checkbox>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { api_login } from '../api/admin.js'
// import instance from "../plugins/http";
// import http from '../plugins/http.js'

export default {
  data() {
    return {
      IsLoading: false,
      form: {
        IsLoading: false,
        username: '',
        password: '',
        rem: false
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.IsLoading = true
      //1、验证表单

      this.$refs.LoginForm.validate(async valid => {
        if (!valid) {
          console.log('error, submit!!!')
          return false
        }

        // 2、提交表单
        const { data: res } = await api_login(
          this.form.username,
          this.form.password
        )
        console.log(api_login(this.form.username, this.form.password))
        // console.log(res)
        this.IsLoading = false
        const data = res.data
        if (res.code !== 200) {
          this.$message.error(res.message)
          return true
        }
        this.$message.success(res.message)
        const token = data.tokenHead + data.token
        // 3、保存token到sessionStorage
        window.sessionStorage.setItem('token', token) //会话结束时，自动消除
        //7、跳转到主页面，编程式的导航
        this.$router.replace('/home')
        //4、保存用户信息到vuex中的store
        //5、记住密码功能，登录成功将用户账户和密码保存到cookies
        if (this.form.rem) {
          this.$cookies.set('username', this.form.username, '7D') //7D表示7天后过期，默认为1D
          this.$cookies.set('password', this.form.password, '7D')
        } else {
          this.$cookies.remove('username')
          this.$cookies.remove('password')
        }
      })
    },
    reset() {
      this.$refs.LoginForm.resetFields()
    }
  },
  created() {
    //6、登录页面刷新或者重新加载时，从cookies中提取相应信息
    //获取cookies中的值
    // console.log("加载成功"+this.form.username+" "+this.form.password+"sad")
    this.form.username = this.$cookies.get('username')
    this.form.password = this.$cookies.get('password')
  }
}
</script>

<style scoped>
/*scoped表示样式只在当前的组件下是有效的*/
.box-card {
  width: 480px;
  margin: auto;
  transform: translateY(50%);
}

.el-button-group {
  width: 100%;
}

.el-button-group .el-button {
  width: 50%;
}
</style>
